﻿//Copyright (c) Microsoft Corporation. All rights reserved.

using Microsoft.Ted.Wacel.Base;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Ted.Wacel
{
    public class InMemoryDataProvider: IDataProvider
    {
        private Dictionary<string, object> mDictionary = new Dictionary<string, object>();
        private DataProviderConfiguration mConfiguration;

        public void Put(string key, object value, string partition = null)
        {
            lock (mDictionary)
            {
                if (!mDictionary.ContainsKey(key))
                    mDictionary.Add(key, value);
                else
                    mDictionary[key] = value;
            }
        }

        public object Get(string key, string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            lock (mDictionary)
            {
                return mDictionary[key];
            }
        }

        public void Update(string key, object value, string partition = null)
        {
            lock (mDictionary)
            {
                if (!mDictionary.ContainsKey(key))
                    mDictionary.Add(key, value);
                else
                    mDictionary[key] = value;
            }
        }

        public void Delete(string key, string partition = null)
        {
            lock (mDictionary)
            {
                mDictionary.Remove(key);
            }
        }

        public IEnumerable<KeyValuePair<string, object>> BulkGet(List<string> keys, string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            lock (mDictionary)
            {
                List<KeyValuePair<string, object>> ret = new List<KeyValuePair<string, object>>();
                foreach(string key in keys)
                {
                    if (mDictionary.ContainsKey(key))
                        ret.Add(new KeyValuePair<string, object>(key, mDictionary[key]));
                }
                return ret;
            }
        }

        public void BulkPut(IEnumerable<KeyValuePair<string, object>> values, string partition = null)
        {
            lock (mDictionary)
            {
                foreach (var item in values)
                    Put(item.Key, item.Value);
            }
        }

        public void Clear()
        {
            lock (mDictionary)
            {
                mDictionary.Clear();
            }
        }

        public DataProviderConfiguration Configuration
        {
            get
            {
                return mConfiguration;
            }
            set
            {
                mConfiguration = value;
            }
        }

        public IEnumerable<KeyValuePair<string, object>> GetAll(string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            lock (mDictionary)
            {
                return mDictionary.ToList();
            }
        }
        public IDataProvider Clone(string newName)
        {
            return new InMemoryDataProvider() { Configuration = mConfiguration };
        }
    }
}
